package com.longrise.android.byjk.plugins.hra.hratabfirst;

import android.animation.ObjectAnimator;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.CalendarView;
import com.longrise.android.byjk.R;
import com.longrise.android.byjk.event.HraDateEvent;
import com.longrise.common.utils.AppUtil;

import org.greenrobot.eventbus.EventBus;

/**
 * Created by callxkj on 2018/1/8.
 * 日历dialog
 */

public class CalendarDialogFragment extends DialogFragment implements View.OnClickListener {

    private static final String TAG = "CalendarDialogFragment";
    private View mRootView;
    private CalendarView mCalendarView;
    private RelativeLayout mRlcalendar;
    private View mCalendarBg;
    private boolean isFirst;
    private int mPageName; //跳转过来的页面
    private TextView mTvdate;
    private LinearLayout mLldata;


    /**
     * 这样写 易于后期维护
     *
     * @param pageName 1. ChangeHraPointActivity     修改服务点
     *                 2. HraOrderEndorseActivity    预约改签
     *                 3. HraOrderFragment           HRA预约
     */
    public static CalendarDialogFragment newInstance(int year, int month, int day, int pageName) {
        CalendarDialogFragment fragment = new CalendarDialogFragment();
        Bundle args = new Bundle();
        args.putInt("year", year);
        args.putInt("month", month);
        args.putInt("day", day);
        args.putInt("pageName", pageName);
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);

        isFirst = true;
        Bundle arguments = getArguments();
        int year = 0;
        int month = 0;
        int day = 0;
        if (arguments != null) {
            year = arguments.getInt("year");
            month = arguments.getInt("month");
            day = arguments.getInt("day");
            mPageName = arguments.getInt("pageName");
        }

        mRootView = inflater.inflate(R.layout.by_dialogfragment_calendar, null);
        mRlcalendar = (RelativeLayout) mRootView.findViewById(R.id.dialogfragment_hra_calendar_rl1);
        mCalendarView = (CalendarView) mRootView.findViewById(R.id.dialogfragment_hra_calendar_calendarView);
        mCalendarBg = mRootView.findViewById(R.id.dialogfragment_hra_calendar_bg_view);
        ImageView ivArrow = (ImageView) mRootView.findViewById(R.id.hra_pinner_header_date_iv);
        ivArrow.setBackgroundResource(R.drawable.by_icon_hra_ic_hra_folder);

        mTvdate = (TextView) mRootView.findViewById(R.id.hra_pinner_header_date_tv);
        if (year != 0) {
            mTvdate.setText(month + "月" + day + "日");
            mCalendarView.scrollToCalendar(year, month, day);
        }

        mLldata = (LinearLayout) mRootView.findViewById(R.id.ll_date_selection);

        return mRootView;

    }

    @Override
    public void onStart() {
        super.onStart();
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.gravity = Gravity.TOP;
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.dimAmount = 0.0f;
        window.setAttributes(params);
        //设置背景透明
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));


        int height = AppUtil.dip2px(400);
        ObjectAnimator animator = ObjectAnimator.ofInt(mCalendarView, "Unfold", 0, height);
        animator.setDuration(250);
        animator.start();

        ObjectAnimator animator2 = ObjectAnimator.ofFloat(mCalendarBg, "Alpha", 0, 1);
        animator2.setDuration(250);
        animator2.start();

        mRlcalendar.setVisibility(View.VISIBLE);

        initData();
        initEvent();
    }

    private void initData() {

    }

    private void initEvent() {
        mCalendarBg.setOnClickListener(this);
        mCalendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
            @Override
            public void onDateChange(Calendar calendar) {
                //设置日历下面的日期选择框
                mCalendarView.setBtmData(calendar.getYear(), calendar.getMonth());
            }

            @Override
            public void onYearChange(int year) {

            }
        });

        mCalendarView.setOnDateSelectedListener(new CalendarView.OnDateSelectedListener() {
            @Override
            public void onDateSelected(Calendar calendar) {
                int year = calendar.getYear();
                int month = calendar.getMonth();
                int day = calendar.getDay();

                if (isFirst) {  //第一次进来的时候 会默认选择一个日期 然后会调用这个方法
                    isFirst = false;
                } else {
                    HraDateEvent event = new HraDateEvent();
                    event.setYear(year);
                    event.setMonth(month);
                    event.setDay(day);
                    event.setPageNum(mPageName);
                    EventBus.getDefault().post(event);
                    getDialog().dismiss();
                }
            }
        });

        mLldata.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getDialog().dismiss();
            }
        });
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.dialogfragment_hra_calendar_bg_view:
                getDialog().dismiss();
                break;

        }
    }

}
